#region Using Statements
using System;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using MercuryParticleEngine;
using MercuryParticleEngine.Emitters;
using MercuryParticleEngine.Modifiers;
using XNAExtras;
#endregion

namespace BumbleGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    /// 

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        
        #region Global Variables
        //## start of global variables ##
        GraphicsDeviceManager graphics;
        ContentManager menuContent;
        ContentManager level1Content;
        ContentManager globalContent;
        SaveGameData saveData;

        
        tagScreenFade ScreenFade;

        Results resultsScreen;
        FlowerTotals flowerTotals; //for keeping track to display on the results screen
        LevelTextures levelTextures; //for specifying which textures to load for the different gardens
        //keeps track of what level is active.  0 should be the main menu, 1 should be level 1, etc...
        int gameState;
        //keeps track of whether game is between levels/states/menus
        bool stateTransition;
        //keeps track of whether the game is paused or not
        bool paused = false;
       
        //game level
        int gameLevel = 0;

        string BGTexture; //to pass the right scrolling background texture into the level load function

        //particle engine junk
        private ParticleSystem system;
        private Emitter emitterLeft;
        private Emitter emitterRight;
        private Emitter emitterLeftConstant;
        private Emitter emitterRightConstant;
        private float fPollenBallScaleFactorPersistent, fPollenBallScaleFactorTriggered;
        //texture class for emitter pollen
        //private Flower PollenBall1, PollenBall2;
        //new pollen list, to be populated in the level
        //private List<PollenBall> GlobalPollenList;
        private List<List<char>> levelMap, bossfliesMap;
        
        //text writing junk
        //public BitmapFont m_fontTimes;
        public SpriteFont font;

        //Texture2D menuTexture;
        //Rectangle menuRectangle;

        KeyboardState oldState;
        GamePadState previousGamePadState;

        AudioEngine audioEngine;
        WaveBank waveBank;
        SoundBank sfxBank, musicBank;
        AudioCategory musicCategory;
        AudioCategory sfxCategory;

        SpriteBatch globalBatch, sprite, sprite2, sprite3, sprite4;

        //for testing only, normally opening screen would go here
        Menu MainMenu;
        int menuSelection, maxMenuSelection;
        bool buttonSelected, menuBack;
        Level1 TestLevel;

        //coordinates to draw the sprite at
        public Viewport viewport;
        
        //moved to Flower.pollenGone
        //bool pollen1Gone = false;
        //bool pollen2Gone = false;
        
      
        private Bumblepig Bumblepig;

        //Timing variables for update and draw
        //private float elapsed;
        //private float totalTime;

        //music cue for playing at beginning of level and stopping at end
        private Cue musicCue;
        private bool bLastEndSequenceState; //For checking when the boss sequence is just beginning so we can start the boss music
        //floats for controlling music and sfx volume
        private float musicVolume, sfxVolume;
        //private Cue buttonSoundCue;

        public static Effect lightEffect;
        public static Effect mask;
        public static Effect pigOffset;
        public static RenderTarget2D renderTarget;
        RenderTarget2D renderTarget2, renderTargetLighter, renderTargetFirespray;
        RenderTarget2D voidTarget;
        public static Texture2D texturedRenderedTo, texturedRenderedToKidHand, texturedRenderedToFirespray, textureRenderedToFullScreen;
        public static List<Texture2D> texturedRenderedToList = new List<Texture2D>();
        public static Texture2D darkFilter;
        public static Texture2D orangeFilter;
        public static Texture2D lightFilter;
        public static int gSaturate;
        //Constants for controlling the saturation level on each stage
        public static int gSaturateDusk = 60;
        public static int gSaturateNight = 0;

        Texture2D lightorb;
        Texture2D bossLighterLightorb;
        Texture2D bossFiresprayLightorb;
        //## end of global variables ##
        #endregion Global Variables

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            menuContent = new ContentManager(Services);
            level1Content = new ContentManager(Services);
            globalContent = new ContentManager(Services);
            saveData = new SaveGameData();
            resultsScreen = new Results();
            flowerTotals = new FlowerTotals();
            levelTextures = new LevelTextures();
            oldState = Keyboard.GetState();

            //BRENT EDIT//
            graphics.MinimumPixelShaderProfile = ShaderProfile.PS_2_0;
            //PollenBall1 = null;
            //PollenBall2 = null;

            gameState = 0; // trying gamestate here
            stateTransition = false;

            Bumblepig = new Bumblepig();
            // for testing only, normally opening screen stuff would go here
            MainMenu = new Menu();
            menuSelection = 1;
            maxMenuSelection = 2;            
            menuBack = false;
            TestLevel = new Level1();            

            //particle engine junk
            emitterLeft = new Emitter(1000);
            emitterRight = new Emitter(1000);
            emitterLeftConstant = new Emitter(500);
            emitterRightConstant = new Emitter(500);
            system = new ParticleSystem(this);
            //Adjust the variables below to control the speed that the pollenball drains off bumblepig's foot
            fPollenBallScaleFactorPersistent = 0.0005f;
            fPollenBallScaleFactorTriggered = 0.002f;

            //GlobalPollenList = new List<PollenBall>();
            //levelMap = levelLayout();
            
            
            //audio engine stuff
            audioEngine = new AudioEngine("Sound/Bumble XACT project.xgs");
            waveBank = new WaveBank(audioEngine, "Sound/Wave Bank.xwb");
            musicBank = new SoundBank(audioEngine, "Sound/Music Bank.xsb");
            sfxBank = new SoundBank(audioEngine, "Sound/SFX Bank.xsb");
            musicCategory = audioEngine.GetCategory("Music");
            sfxCategory = audioEngine.GetCategory("SFX");
            musicVolume = 1.0f;
            sfxVolume = 1.0f;
            bLastEndSequenceState = false;


            //should probably move music cue to level 1
            //soundBank.PlayCue("Gloria rough");
            
            //Timing stuff for update and draw
            //elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            //totalTime = 0;

            //m_fontTimes = new BitmapFont("Fonts/times.xml", menuContent); //text writing junk

            
            
        }
        //potentially useful for top end of random scale modifier on emitters... we should probably just make a new modifier
        
        
        protected override void Initialize()
        {
            //keep this stuff here
            base.Initialize();
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;

            ScreenFade.Initialize(globalContent, graphics.GraphicsDevice);
            
            //runs like crap
            //graphics.IsFullScreen = true;
            graphics.ApplyChanges();
            //viewport = graphics.GraphicsDevice.Viewport;
            //viewport.Height = 768;
            //viewport.Width = 1024;
            
            //particle engine junk      
            system.BlendMode = SpriteBlendMode.AlphaBlend;
            //BRENT EDIT//
            gSaturate = 0;
           
            //setting gameState to 1, which loads level 1.... for testing purposes only
            //gameState = 1;
            //BRENT EDIT//
            renderTarget = new RenderTarget2D(graphics.GraphicsDevice, 1024, 768, 1, SurfaceFormat.Color);
            renderTarget2 = new RenderTarget2D(graphics.GraphicsDevice, 1024, 256, 1, SurfaceFormat.Color);
            renderTargetLighter = new RenderTarget2D(graphics.GraphicsDevice, 256, 256, 1, SurfaceFormat.Color);
            renderTargetFirespray = new RenderTarget2D(graphics.GraphicsDevice, 256, 512, 1, SurfaceFormat.Color);
            voidTarget = new RenderTarget2D(graphics.GraphicsDevice, 200, 200, 1, SurfaceFormat.Color);
            
        }
        
                        
        protected override void LoadContent()
        {
            //BRENT EDIT//
            lightEffect = globalContent.Load<Effect>("light");
            mask = globalContent.Load<Effect>("mask");

            //text stuff
            font = globalContent.Load<SpriteFont>("Arial");

            viewport = graphics.GraphicsDevice.Viewport;
            //setting height and width manually because backbufferwidth is a piece of shit
            //needs to be addressed at some point
            viewport.Height = 768;
            viewport.Width = 1024;
            //trying pollenball init here
            //PollenBall1 = new Flower(viewport.Height, TestLevel.PollenBall1Placements, TestLevel.PollenBall1Times, 0);
            //PollenBall2 = new Flower(viewport.Height, TestLevel.PollenBall2Placements, TestLevel.PollenBall2Times, 0);

            globalBatch = new SpriteBatch(graphics.GraphicsDevice);
            sprite = new SpriteBatch(graphics.GraphicsDevice);
            sprite2 = new SpriteBatch(graphics.GraphicsDevice);
            sprite3 = new SpriteBatch(graphics.GraphicsDevice);
            sprite4 = new SpriteBatch(graphics.GraphicsDevice);
            //m_fontTimes.Reset(graphics.GraphicsDevice, globalBatch); //text writing junk

            //moved gamestate change here to try and narrow down pollenball = null issue.
            //gameState = 1;
            // Load level 1, for testing purposes.  Normally the menu info / opening screen would load here
            if (gameState == 0)
            {
                MainMenu.LoadMenu(graphics, menuContent, viewport);                    
                musicCue = musicBank.GetCue("BumbleMainMenu");
                //I think playing the music here is useless, since we do it again during the state transition... and it may be causing a bug too...
                //musicCue.Play();
                stateTransition = true;
                //menuTexture = menuContent.Load<Texture2D>("images/bug");
                //menuRectangle = new Rectangle(0, 0, menuTexture.Width, menuTexture.Height);
            }
            //BRENT EDIT//
            lightorb = globalContent.Load<Texture2D>("Images/light_orb");
            bossLighterLightorb = globalContent.Load<Texture2D>("Images/boss_lighter2_lightorb");
            bossFiresprayLightorb = globalContent.Load<Texture2D>("Images/boss_fire_oneframe_lightorb");
            darkFilter = globalContent.Load<Texture2D>("Images/blackfilter");
            orangeFilter = globalContent.Load<Texture2D>("Images/dusk_red40");
            lightFilter = globalContent.Load<Texture2D>("Images/light_orb2");
        }
        
        protected override void UnloadContent()
        {            
            menuContent.Unload();
            level1Content.Unload();            
        }

        #region Update
        protected override void Update(GameTime gameTime)
        {

            // Allows the default game to exit on Xbox 360 and Windows
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
           
            // The time since Update was called last
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            //totalTime = totalTime + elapsed;//now that we're reading from a file to populate the level, not sure we need total time anywhere
            if (stateTransition == true)
            {
                if (gameState == 0)
                {
                    //saveData = Load();
                    Bumblepig.coins = saveData.coins;
                    if (Bumblepig.coins > 0) Bumblepig.floatcoins = (float)Bumblepig.coins / 100;
                    musicCue.Stop(AudioStopOptions.Immediate);
                    musicCue = musicBank.GetCue("BumbleMainMenu");
                    musicCue.Play();
                    
                    stateTransition = false;
                }

                if (gameState == 1)
                {
                    //gameLevel = 2;
                    switch (gameLevel) //load the level layout for the right level
                    {
                        //level 1
                        case 0: 
                            levelMap = levelLayout("bumblepig_level_leveltest.txt");
                            break;
                        //level 2
                        case 1: levelMap = levelLayout("bumblepig_level_leveltest.txt");
                            break;
                        //level 3
                        case 2: 
                            levelMap = levelLayout("bumblepig_level_leveltest.txt");
                            bossfliesMap = levelLayout("bumblepig_bossflies_layout.txt");
                            break;
                        //level 4
                        case 3:
                            gameLevel = 0;
                            gameState = 0;
                            break;
                        //level 5
                        case 4: levelMap = levelLayout("bumblepig_level_leveltest.txt");
                            break;
                        //level 6
                        case 5: levelMap = levelLayout("bumblepig_level_leveltest.txt");
                            break;
                        //level 7
                        case 6: levelMap = levelLayout("bumblepig_level_leveltest.txt");
                            break;
                        //level 8
                        case 7: levelMap = levelLayout("bumblepig_level_leveltest.txt");
                            break;
                        default:
                            levelMap = levelLayout("bumblepig_level_leveltest.txt");
                            bossfliesMap = levelLayout("bumblepig_bossflies_layout.txt");
                            break;
                    }
                    //now the image paths, etc...
                    #region garden1
                    //load the little girl garden textures for the first 3 levels
                    if (gameLevel == 0 || gameLevel == 1 || gameLevel == 2)
                    {
                        //flower image paths
                        levelTextures.blueFlowerFemale = "Images/Flowers/flower_blue01a";
                        levelTextures.blueFlowerMale = "Images/Flowers/flowerm_blue01a";
                        levelTextures.redFlowerFemale = "Images/Flowers/flower_red01a";
                        levelTextures.redFlowerMale = "Images/Flowers/flowerm_red01a";
                        levelTextures.yellowFlowerFemale = "Images/Flowers/flower_yellow01a";
                        levelTextures.yellowFlowerMale = "Images/Flowers/flowerm_yellow01a";
                        //Main background texture
                        BGTexture = "Images/BGElements/background01c";
                        //now all the little added background elements... and animation info... ugh
                        /*levelTextures.BGElement0 = "Images/BGElements/...";
                        levelTextures.BGElement0framesPerSec = 1;
                        levelTextures.BGElement0framesTall = 1;
                        levelTextures.BGElement0framesWide = 1;
                        levelTextures.BGElement0lastFrame = 1;
                        levelTextures.BGElement0sym = false;

                        levelTextures.BGElement1 = "Images/BGElements/...";
                        levelTextures.BGElement1framesPerSec = 1;
                        levelTextures.BGElement1framesTall = 1;
                        levelTextures.BGElement1framesWide = 1;
                        levelTextures.BGElement1lastFrame = 1;
                        levelTextures.BGElement1sym = false;

                        levelTextures.BGElement2 = "Images/BGElements/...";
                        levelTextures.BGElement2framesPerSec = 1;
                        levelTextures.BGElement2framesTall = 1;
                        levelTextures.BGElement2framesWide = 1;
                        levelTextures.BGElement2lastFrame = 1;
                        levelTextures.BGElement2sym = false;

                        levelTextures.BGElement3 = "Images/BGElements/...";
                        levelTextures.BGElement3framesPerSec = 1;
                        levelTextures.BGElement3framesTall = 1;
                        levelTextures.BGElement3framesWide = 1;
                        levelTextures.BGElement3lastFrame = 1;
                        levelTextures.BGElement3sym = false;

                        levelTextures.BGElement4 = "Images/BGElements/...";
                        levelTextures.BGElement4framesPerSec = 1;
                        levelTextures.BGElement4framesTall = 1;
                        levelTextures.BGElement4framesWide = 1;
                        levelTextures.BGElement4lastFrame = 1;
                        levelTextures.BGElement4sym = false;

                        levelTextures.BGElement5 = "Images/BGElements/...";
                        levelTextures.BGElement5framesPerSec = 1;
                        levelTextures.BGElement5framesTall = 1;
                        levelTextures.BGElement5framesWide = 1;
                        levelTextures.BGElement5lastFrame = 1;
                        levelTextures.BGElement5sym = false;

                        levelTextures.BGElement6 = "Images/BGElements/...";
                        levelTextures.BGElement6framesPerSec = 1;
                        levelTextures.BGElement6framesTall = 1;
                        levelTextures.BGElement6framesWide = 1;
                        levelTextures.BGElement6lastFrame = 1;
                        levelTextures.BGElement6sym = false;

                        levelTextures.BGElement7 = "Images/BGElements/...";
                        levelTextures.BGElement7framesPerSec = 1;
                        levelTextures.BGElement7framesTall = 1;
                        levelTextures.BGElement7framesWide = 1;
                        levelTextures.BGElement7lastFrame = 1;
                        levelTextures.BGElement7sym = false;

                        levelTextures.BGElement8 = "Images/BGElements/...";
                        levelTextures.BGElement8framesPerSec = 1;
                        levelTextures.BGElement8framesTall = 1;
                        levelTextures.BGElement8framesWide = 1;
                        levelTextures.BGElement8lastFrame = 1;
                        levelTextures.BGElement8sym = false;

                        levelTextures.BGElement9 = "Images/BGElements/...";
                        levelTextures.BGElement9framesPerSec = 1;
                        levelTextures.BGElement9framesTall = 1;
                        levelTextures.BGElement9framesWide = 1;
                        levelTextures.BGElement9lastFrame = 1;
                        levelTextures.BGElement9sym = false;*/
                    }
                    #endregion garden1
                    #region garden2
                    //load the water garden textures for the second 3 levels
                    if (gameLevel == 3 || gameLevel == 4 || gameLevel == 5)
                    {
                        //flower image paths
                        levelTextures.blueFlowerFemale = "Images/Flowers/flower_blue01";
                        levelTextures.blueFlowerMale = "Images/Flowers/flowerm_blue01";
                        levelTextures.redFlowerFemale = "Images/Flowers/flower_red01";
                        levelTextures.redFlowerMale = "Images/Flowers/flowerm_red01";
                        levelTextures.yellowFlowerFemale = "Images/Flowers/flower_yellow02";
                        levelTextures.yellowFlowerMale = "Images/Flowers/flowerm_yellow02";
                        //Main background texture
                        BGTexture = "Images/BGElements/background01c";
                        //now all the little added background elements... and animation info... ugh
                        /*levelTextures.BGElement0 = "Images/BGElements/...";
                        levelTextures.BGElement0framesPerSec = 1;
                        levelTextures.BGElement0framesTall = 1;
                        levelTextures.BGElement0framesWide = 1;
                        levelTextures.BGElement0lastFrame = 1;
                        levelTextures.BGElement0sym = false;

                        levelTextures.BGElement1 = "Images/BGElements/...";
                        levelTextures.BGElement1framesPerSec = 1;
                        levelTextures.BGElement1framesTall = 1;
                        levelTextures.BGElement1framesWide = 1;
                        levelTextures.BGElement1lastFrame = 1;
                        levelTextures.BGElement1sym = false;

                        levelTextures.BGElement2 = "Images/BGElements/...";
                        levelTextures.BGElement2framesPerSec = 1;
                        levelTextures.BGElement2framesTall = 1;
                        levelTextures.BGElement2framesWide = 1;
                        levelTextures.BGElement2lastFrame = 1;
                        levelTextures.BGElement2sym = false;

                        levelTextures.BGElement3 = "Images/BGElements/...";
                        levelTextures.BGElement3framesPerSec = 1;
                        levelTextures.BGElement3framesTall = 1;
                        levelTextures.BGElement3framesWide = 1;
                        levelTextures.BGElement3lastFrame = 1;
                        levelTextures.BGElement3sym = false;

                        levelTextures.BGElement4 = "Images/BGElements/...";
                        levelTextures.BGElement4framesPerSec = 1;
                        levelTextures.BGElement4framesTall = 1;
                        levelTextures.BGElement4framesWide = 1;
                        levelTextures.BGElement4lastFrame = 1;
                        levelTextures.BGElement4sym = false;

                        levelTextures.BGElement5 = "Images/BGElements/...";
                        levelTextures.BGElement5framesPerSec = 1;
                        levelTextures.BGElement5framesTall = 1;
                        levelTextures.BGElement5framesWide = 1;
                        levelTextures.BGElement5lastFrame = 1;
                        levelTextures.BGElement5sym = false;

                        levelTextures.BGElement6 = "Images/BGElements/...";
                        levelTextures.BGElement6framesPerSec = 1;
                        levelTextures.BGElement6framesTall = 1;
                        levelTextures.BGElement6framesWide = 1;
                        levelTextures.BGElement6lastFrame = 1;
                        levelTextures.BGElement6sym = false;

                        levelTextures.BGElement7 = "Images/BGElements/...";
                        levelTextures.BGElement7framesPerSec = 1;
                        levelTextures.BGElement7framesTall = 1;
                        levelTextures.BGElement7framesWide = 1;
                        levelTextures.BGElement7lastFrame = 1;
                        levelTextures.BGElement7sym = false;

                        levelTextures.BGElement8 = "Images/BGElements/...";
                        levelTextures.BGElement8framesPerSec = 1;
                        levelTextures.BGElement8framesTall = 1;
                        levelTextures.BGElement8framesWide = 1;
                        levelTextures.BGElement8lastFrame = 1;
                        levelTextures.BGElement8sym = false;

                        levelTextures.BGElement9 = "Images/BGElements/...";
                        levelTextures.BGElement9framesPerSec = 1;
                        levelTextures.BGElement9framesTall = 1;
                        levelTextures.BGElement9framesWide = 1;
                        levelTextures.BGElement9lastFrame = 1;
                        levelTextures.BGElement9sym = false;*/
                    }
                    #endregion garden2
                    #region garden3
                    //load the jungle garden textures for the last 3 levels
                    if (gameLevel == 6 || gameLevel == 7 || gameLevel == 8)
                    {
                        //flower image paths
                        levelTextures.blueFlowerFemale = "Images/Flowers/flower_blue01";
                        levelTextures.blueFlowerMale = "Images/Flowers/flowerm_blue01";
                        levelTextures.redFlowerFemale = "Images/Flowers/flower_red01";
                        levelTextures.redFlowerMale = "Images/Flowers/flowerm_red01";
                        levelTextures.yellowFlowerFemale = "Images/Flowers/flower_yellow02";
                        levelTextures.yellowFlowerMale = "Images/Flowers/flowerm_yellow02";
                        //Main background texture
                        BGTexture = "Images/BGElements/background01c";
                        //now all the little added background elements... and animation info... ugh
                        levelTextures.BGElement0 = "Images/BGElements/...";
                        levelTextures.BGElement0framesPerSec = 1;
                        levelTextures.BGElement0framesTall = 1;
                        levelTextures.BGElement0framesWide = 1;
                        levelTextures.BGElement0lastFrame = 1;
                        levelTextures.BGElement0sym = false;

                        levelTextures.BGElement1 = "Images/BGElements/...";
                        levelTextures.BGElement1framesPerSec = 1;
                        levelTextures.BGElement1framesTall = 1;
                        levelTextures.BGElement1framesWide = 1;
                        levelTextures.BGElement1lastFrame = 1;
                        levelTextures.BGElement1sym = false;

                        levelTextures.BGElement2 = "Images/BGElements/...";
                        levelTextures.BGElement2framesPerSec = 1;
                        levelTextures.BGElement2framesTall = 1;
                        levelTextures.BGElement2framesWide = 1;
                        levelTextures.BGElement2lastFrame = 1;
                        levelTextures.BGElement2sym = false;

                        levelTextures.BGElement3 = "Images/BGElements/...";
                        levelTextures.BGElement3framesPerSec = 1;
                        levelTextures.BGElement3framesTall = 1;
                        levelTextures.BGElement3framesWide = 1;
                        levelTextures.BGElement3lastFrame = 1;
                        levelTextures.BGElement3sym = false;

                        levelTextures.BGElement4 = "Images/BGElements/...";
                        levelTextures.BGElement4framesPerSec = 1;
                        levelTextures.BGElement4framesTall = 1;
                        levelTextures.BGElement4framesWide = 1;
                        levelTextures.BGElement4lastFrame = 1;
                        levelTextures.BGElement4sym = false;

                        levelTextures.BGElement5 = "Images/BGElements/...";
                        levelTextures.BGElement5framesPerSec = 1;
                        levelTextures.BGElement5framesTall = 1;
                        levelTextures.BGElement5framesWide = 1;
                        levelTextures.BGElement5lastFrame = 1;
                        levelTextures.BGElement5sym = false;

                        levelTextures.BGElement6 = "Images/BGElements/...";
                        levelTextures.BGElement6framesPerSec = 1;
                        levelTextures.BGElement6framesTall = 1;
                        levelTextures.BGElement6framesWide = 1;
                        levelTextures.BGElement6lastFrame = 1;
                        levelTextures.BGElement6sym = false;

                        levelTextures.BGElement7 = "Images/BGElements/...";
                        levelTextures.BGElement7framesPerSec = 1;
                        levelTextures.BGElement7framesTall = 1;
                        levelTextures.BGElement7framesWide = 1;
                        levelTextures.BGElement7lastFrame = 1;
                        levelTextures.BGElement7sym = false;

                        levelTextures.BGElement8 = "Images/BGElements/...";
                        levelTextures.BGElement8framesPerSec = 1;
                        levelTextures.BGElement8framesTall = 1;
                        levelTextures.BGElement8framesWide = 1;
                        levelTextures.BGElement8lastFrame = 1;
                        levelTextures.BGElement8sym = false;

                        levelTextures.BGElement9 = "Images/BGElements/...";
                        levelTextures.BGElement9framesPerSec = 1;
                        levelTextures.BGElement9framesTall = 1;
                        levelTextures.BGElement9framesWide = 1;
                        levelTextures.BGElement9lastFrame = 1;
                        levelTextures.BGElement9sym = false;
                    }
                    #endregion garden3
                    TestLevel.Load(viewport, level1Content, graphics, Bumblepig, emitterLeft, emitterRight, emitterLeftConstant, emitterRightConstant, system, ref ScreenFade, BGTexture, gameLevel);//, ref GlobalPollenList); //PollenBall1, PollenBall2);
                    //moved to beginning of game, save data needs to be accessed from the stats menu
                    //saveData = Load();
                    //Bumblepig.coins = saveData.coins;
                    Bumblepig.life = 6;
                    musicCue.Stop(AudioStopOptions.Immediate);
                    if (gameLevel == 0 || gameLevel == 3 || gameLevel == 6)
                    {
                        musicCue = musicBank.GetCue("BumbleDay1");
                    }
                    if (gameLevel == 1 || gameLevel == 4 || gameLevel == 7)
                    {
                        musicCue = musicBank.GetCue("BumbleDusk1");
                    }
                    if (gameLevel == 2 || gameLevel == 5 || gameLevel == 8)
                    {
                        musicCue = musicBank.GetCue("BumbleNight1"); 
                    }
                    if (gameState != 0)
                    {
                        musicCue.Play();
                        stateTransition = false;
                        paused = false;
                    }
                }

                if (gameState == 2) // load the results screen
                {

                    saveData.coins = Bumblepig.coins;
                    saveData.life = Bumblepig.life;
                    //Save(saveData, Bumblepig.score);
                    resultsScreen.Load(graphics, menuContent, viewport, flowerTotals);
                    musicCue.Stop(AudioStopOptions.Immediate);
                    musicCue = musicBank.GetCue("BumbleMainMenu");
                    musicCue.Play();
                    stateTransition = false;
                }
            }            
                
                if (gameState == 0 && !stateTransition)
                {
                    MainMenu.UpdateMenu(ref menuSelection, ref maxMenuSelection, ref buttonSelected, ref gameState, ref stateTransition, ref menuBack, ref ScreenFade, ref musicVolume);
                }

                //Update level 1
                if (gameState == 1 && !stateTransition && !paused)
                {
                    TestLevel.Update(elapsed, Bumblepig, emitterLeft, emitterRight, emitterLeftConstant, emitterRightConstant, sfxBank, levelMap, bossfliesMap, graphics, level1Content, viewport, ref gameState, ref flowerTotals, ref ScreenFade, levelTextures, ref musicVolume);
                    foreach (PollenBall pollenBall in TestLevel.PollenList) //need to change from level one specific pollen list
                    {
                        if (pollenBall.pollenState == 1 && pollenBall.isInPlay)
                        {
                            pollenBall.SpriteScale -= fPollenBallScaleFactorPersistent;
                            emitterLeftConstant.Trigger();
                            emitterLeftConstant._scale = 0.1f;
                        }
                        if (pollenBall.pollenState == 2 && pollenBall.isInPlay)
                        {
                            pollenBall.SpriteScale -= fPollenBallScaleFactorPersistent;
                            emitterRightConstant.Trigger();
                            emitterRightConstant._scale = 0.1f;
                        }
                    }
                    if (gameState == 2) stateTransition = true;
                }
                // if rotation is ~0 then draw pollen balls in the standard spot
                if (gameState == 1 && !stateTransition && !paused)
                {
                    if (!Bumblepig.dying)
                    {
                        Bumblepig.RotationCorrection(emitterLeft, emitterRight, emitterLeftConstant, emitterRightConstant);
                    }
                }

                if (gameState == 2 && !stateTransition)
                {
                    resultsScreen.Update(ref flowerTotals, elapsed);
                }
            // Check for user input
            UpdateInput(gameTime, ref ScreenFade);
            
            //The code below was my crude attempt at forcing the music to loop... I have now hacked into .xap and set the music files to loop infinitely, 
            //but there's still an issue with XACT that it won't allow me to set the loop count in the interface like normal...
            //if (musicCue.IsStopped)
            //{
            //    if (gameState == 0)
            //    {
            //        musicCue = musicBank.GetCue("BumbleMainMenu");
                
            //    }
            //    if (gameState == 1)
            //    {
            //        if (gameLevel == 0 || gameLevel == 3 || gameLevel == 6)
            //        {
            //            musicCue = musicBank.GetCue("BumbleDay1");
            //        }
            //        if (gameLevel == 1 || gameLevel == 4 || gameLevel == 7)
            //        {
            //            musicCue = musicBank.GetCue("BumbleDusk1");
            //        }
            //        if (gameLevel == 2 || gameLevel == 5 || gameLevel == 8)
            //        {
            //            musicCue = musicBank.GetCue("BumbleNight1");
            //            if (TestLevel.endsequence) musicCue = musicBank.GetCue("Bumble Boss 1_2"); //loop boss music rather than the normal music if you're at the boss fight
            //        }
            //    }
            //    if (gameState == 2)
            //    {
            //        musicCue = musicBank.GetCue("BumbleMainMenu");

            //    }
            //    musicCue.Play();
            //}
            //Here we stop the normal music and start the boss music if you're at the end of the level with the boss
            if (gameState == 1 && gameLevel == 2 && TestLevel.endsequence && TestLevel.endsequence != bLastEndSequenceState) 
            {
                musicCue.Stop(AudioStopOptions.AsAuthored);
                musicCue = musicBank.GetCue("Bumble Boss 1_2");//Boss music kicks in at the end...
                musicCue.Play();
            }
            bLastEndSequenceState = TestLevel.endsequence; //This is to help tell whether or not we just transitioned into the boss fight, so we can call the boss music to start playing just once
            //update audio
            musicCategory.SetVolume(musicVolume);
            sfxCategory.SetVolume(sfxVolume);
            audioEngine.Update();
            ScreenFade.Update(elapsed, ref gameState, ref stateTransition, ref level1Content, ref musicVolume);
            base.Update(gameTime);
        }



        private void UpdateInput(GameTime gameTime, ref tagScreenFade ScreenFade)
        {
            KeyboardState newState = Keyboard.GetState();
            GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);

            if (!stateTransition)
            {
                //start of in-game controls
                if (gameState == 1 && !TestLevel.isBossDefeated)
                {
                    //pressing escape while in level returns user to main menu
                    //gamepad game exit needs some work, sends user from gameplay back to windows without hitting the menu first
                    if ((newState.IsKeyUp(Keys.Escape) && oldState.IsKeyDown(Keys.Escape)) ||
                        (currentGamePadState.Buttons.B == ButtonState.Released && previousGamePadState.Buttons.B == ButtonState.Pressed))
                    {
                        //if (gameState == 1)
                        //{
                        //    level1Content.Unload();
                        //}
                        //musicCue.Stop(AudioStopOptions.Immediate);//We should put music fading in and out with he screen fading in and out instead of calling this here
                        ScreenFade.Begin(Screen.Menu, FadeType.FadeOut, 1.0f, musicVolume);
                        //gameState = 0;
                        //stateTransition = true;                            
                        saveData.coins = Bumblepig.coins;
                        saveData.life = Bumblepig.life;
                        //Save(saveData, Bumblepig.score);
                        //may need a transition here at some point
                    }
                    if (newState.IsKeyUp(Keys.F1) && oldState.IsKeyDown(Keys.F1))
                    {
                        level1Content.Unload();
                        stateTransition = true;
                        gameLevel = 1;
                    }

                    //Unpauses if 'P' is pressed
                    if (paused && ((newState.IsKeyDown(Keys.P) && oldState.IsKeyUp(Keys.P)) ||
                        ((currentGamePadState.Buttons.Start == ButtonState.Pressed) && (previousGamePadState.Buttons.Start == ButtonState.Released))))
                    {
                        paused = false;
                    }
                    else
                    {
                        //Pauses if 'P' is pressed
                        if (!paused && (newState.IsKeyDown(Keys.P) && oldState.IsKeyUp(Keys.P) ||
                            ((currentGamePadState.Buttons.Start == ButtonState.Pressed) && (previousGamePadState.Buttons.Start == ButtonState.Released))))
                        {
                            paused = true;
                        }
                    }

                    //start nontransition in-game controls, excludes escape
                    #region Ingame Controls
                    if (!stateTransition && !paused)
                    {
                        //playfield boundaries
                        int MaxX = graphics.GraphicsDevice.Viewport.Width - (Bumblepig.BumbleBody.aTexture.Width / 16);
                        int MinX = (Bumblepig.BumbleBody.aTexture.Width / 16);
                        int MaxY = graphics.GraphicsDevice.Viewport.Height - (Bumblepig.BumbleBody.aTexture.Height / 2);
                        int MinY = (Bumblepig.BumbleBody.aTexture.Height / 2);

                        //play controls
                        if (Bumblepig.dead && (newState.IsKeyDown(Keys.Enter) || currentGamePadState.Buttons.Y == ButtonState.Pressed)) //new pig when dead and pressing enter
                        {
                            Bumblepig.newPig = true;
                        }

                        if (!Bumblepig.dead)
                        {
                            //moves bumblepig up
                            if (newState.IsKeyDown(Keys.Up) || (currentGamePadState.ThumbSticks.Left.Y > 0.1f))
                            {
                                if (Bumblepig.spritePosition.Y > MinY)
                                {
                                    
                                    if (TestLevel.nConfidenceMode > 1)
                                    { Bumblepig.spritePosition.Y -= 2; }

                                    //up is positive with the thumbstick and negative with the y-coords, so -= is used
                                    if (currentGamePadState.IsConnected)
                                    {
                                        Bumblepig.spritePosition.Y -= (6 * currentGamePadState.ThumbSticks.Left.Y);
                                        
                                    }
                                    else
                                    {
                                        Bumblepig.spritePosition.Y -= 6;
                                        
                                    }
                                }
                            }

                            //moves bumblepig down
                            if (newState.IsKeyDown(Keys.Down) || (currentGamePadState.ThumbSticks.Left.Y < -0.1f))
                            {
                                if (Bumblepig.spritePosition.Y < MaxY)
                                {
                                    
                                    if (TestLevel.nConfidenceMode > 1)
                                    { Bumblepig.spritePosition.Y += 2; }
                                    
                                    //down is negative with the thumbstick and positive with y-coords, so -= is used
                                    if (currentGamePadState.IsConnected)
                                    {
                                        Bumblepig.spritePosition.Y -= (6 * currentGamePadState.ThumbSticks.Left.Y);
                                        
                                    }
                                    else
                                    {
                                        Bumblepig.spritePosition.Y += 6;
                                        
                                    }
                                }
                            }

                            //moves bumblepig left
                            if (newState.IsKeyDown(Keys.Left) || (currentGamePadState.ThumbSticks.Left.X < -0.1f))
                            {
                                if (Bumblepig.spritePosition.X > MinX)
                                {
                                    
                                    if (TestLevel.nConfidenceMode > 1)
                                    { Bumblepig.spritePosition.X -= 2; }

                                    //left is negative with the thumbstick and x-coords, so += is used
                                    if (currentGamePadState.IsConnected)
                                    {
                                        Bumblepig.spritePosition.X += (6 * currentGamePadState.ThumbSticks.Left.X);
                                        
                                    }
                                    else
                                    {
                                        Bumblepig.spritePosition.X -= 6;
                                        
                                    }

                                    if (Bumblepig.spriteRotation > (0.6f * currentGamePadState.ThumbSticks.Left.X))
                                    {
                                        Bumblepig.spriteRotation = (0.6f * currentGamePadState.ThumbSticks.Left.X);
                                        
                                    }

                                }
                            }

                            //moves bumblepig right
                            if (newState.IsKeyDown(Keys.Right) || (currentGamePadState.ThumbSticks.Left.X > 0.1f))
                            {
                                if (Bumblepig.spritePosition.X < MaxX)
                                {
                                    
                                    if (TestLevel.nConfidenceMode > 1)
                                    { Bumblepig.spritePosition.X += 2; }

                                    //right is positive with the thumbstick and x-coords, so += is used
                                    if (currentGamePadState.IsConnected)
                                    {
                                        Bumblepig.spritePosition.X += (6 * currentGamePadState.ThumbSticks.Left.X);
                                        
                                    }
                                    else
                                    {
                                        Bumblepig.spritePosition.X += 6;
                                        
                                    }


                                    if (Bumblepig.spriteRotation < (0.6f * currentGamePadState.ThumbSticks.Left.X))
                                    {
                                        Bumblepig.spriteRotation = (0.6f * currentGamePadState.ThumbSticks.Left.X);
                                        
                                    }
                                }
                            }

                            //rotates bumblepig back to the right after left is released
                            //had to detect gamepad connected state, because keys are always up when using the gamepad
                            if ((newState.IsKeyUp(Keys.Left) && !currentGamePadState.IsConnected)
                                || (currentGamePadState.IsConnected && currentGamePadState.ThumbSticks.Left.X > -0.1f))
                            {


                                if (Bumblepig.spriteRotation < 0.0f)
                                {
                                    Bumblepig.spriteRotation += 0.1f;
                                    
                                }

                            }

                            //rotates bumblepig back to the left after right is released
                            if ((newState.IsKeyUp(Keys.Right) && !currentGamePadState.IsConnected)
                                || (currentGamePadState.IsConnected && currentGamePadState.ThumbSticks.Left.X < 0.1f))
                            {
                                if (Bumblepig.spriteRotation > 0.0f)
                                {
                                    Bumblepig.spriteRotation -= 0.1f;
                                    
                                }
                            }

                            //don't need gamepad controls for this, cant press left and right on the analog stick at the same time
                            if (newState.IsKeyDown(Keys.Right) && newState.IsKeyDown(Keys.Left))
                            {
                                if (Bumblepig.spriteRotation > 0)
                                {
                                    Bumblepig.spriteRotation -= 0.1f;
                                   
                                }
                                if (Bumblepig.spriteRotation < 0)
                                {
                                    Bumblepig.spriteRotation += 0.1f;
                                    
                                }

                            }


                            //diving pig
                            //maybe phasing this out, dont worry about gamepad input here
                            if (newState.IsKeyDown(Keys.Space))
                            {
                                Bumblepig.onTopLayer = false;
                                if (Bumblepig.spriteScale > 0.5f && !Bumblepig.dead)
                                {
                                    Bumblepig.spriteScale -= 0.01f;
                                    Bumblepig.diveShadow -= 2;
                                }
                            }

                            if (newState.IsKeyUp(Keys.Space))
                            {
                                if (Bumblepig.spriteScale == 1)
                                {
                                    Bumblepig.onTopLayer = true;
                                }
                                if (Bumblepig.spriteScale < 1f && !Bumblepig.dying && !Bumblepig.dead)
                                {
                                    Bumblepig.spriteScale += 0.01f;
                                    Bumblepig.diveShadow += 2;
                                }

                                if (Bumblepig.spriteScale > 0.99)
                                {
                                    Bumblepig.spriteScale = 1.0f;
                                    Bumblepig.diveShadow = 113;
                                }
                            }
                        }


                        //pollen shrinking when you hold down the button
                        //left pollen trigger
                        if (newState.IsKeyDown(Keys.Z) || currentGamePadState.Triggers.Left > 0.1f)
                        {
                            foreach (PollenBall pollenBall in TestLevel.PollenList) //need to change from level one specific pollen list
                            {
                                if (pollenBall.pollenState == 1 && pollenBall.isInPlay)
                                {
                                    pollenBall.SpriteScale -= fPollenBallScaleFactorTriggered;
                                    emitterLeft.Trigger();
                                    emitterLeft._scale = 0.2f;
                                }
                            }
                        }

                        //right pollen trigger
                        if (newState.IsKeyDown(Keys.X) || currentGamePadState.Triggers.Right > 0.1f)
                        {
                            foreach (PollenBall pollenBall in TestLevel.PollenList)
                            {
                                if (pollenBall.pollenState == 2 && pollenBall.isInPlay)
                                {
                                    pollenBall.SpriteScale -= fPollenBallScaleFactorTriggered;
                                    emitterRight.Trigger();
                                    emitterRight._scale = 0.2f;
                                }
                            }
                        }

                        //releases left pollen ball
                        if ((newState.IsKeyDown(Keys.A) && oldState.IsKeyUp(Keys.A)) ||
                            (currentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed && previousGamePadState.Buttons.LeftShoulder == ButtonState.Released))
                        {
                            foreach (PollenBall pollenBall in TestLevel.PollenList)
                            {
                                if (pollenBall.pollenState == 1 && pollenBall.isInPlay)
                                {
                                    pollenBall.pollenState = 3;
                                    pollenBall.transitionLeft = true;
                                    pollenBall.isOnFoot = false;
                                }
                            }
                        }

                        //releases right pollen ball
                        if ((newState.IsKeyDown(Keys.S) && oldState.IsKeyUp(Keys.S)) ||
                        (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed && previousGamePadState.Buttons.RightShoulder == ButtonState.Released))
                        {
                            foreach (PollenBall pollenBall in TestLevel.PollenList)
                            {
                                if (pollenBall.pollenState == 2 && pollenBall.isInPlay)
                                {
                                    pollenBall.pollenState = 3;
                                    pollenBall.transitionRight = true;
                                    pollenBall.isOnFoot = false;
                                }
                            }
                        }

                        //shooting from the nose
                        if ((newState.IsKeyDown(Keys.RightControl) && !oldState.IsKeyDown(Keys.RightControl)) || 
                            (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A != ButtonState.Pressed))
                        {
                            
                            if (TestLevel.nConfidenceMode > 0 && Bumblepig.bulletDestroyed)
                            { 
                                Bumblepig.bulletDestroyed = false;
                                Bumblepig.Bullet.SpriteRotation = Bumblepig.spriteRotation;
                            }
                            //Make the bullet explode here if shoot button is pressed while the bullet is flying
                            else if (TestLevel.nConfidenceMode > 2 && !Bumblepig.bulletDestroyed)
                            {
                                //Here we need call a function so that the Bumblepig class can handle the exploding bullet thing
                                Bumblepig.ExplodeBullet();
                            }
                        }          
         
                        //exploding the nose ball
                        if((newState.IsKeyDown(Keys.RightShift) && !oldState.IsKeyDown(Keys.RightShift)) ||
                            (currentGamePadState.Buttons.X == ButtonState.Pressed && previousGamePadState.Buttons.X != ButtonState.Pressed))
                        {                            
                            //Make the bullet explode here if shoot button is pressed while the bullet is flying
                            if (TestLevel.nConfidenceMode > 2 && !Bumblepig.bulletDestroyed)
                            {
                                //Here we need call a function so that the Bumblepig class can handle the exploding bullet thing
                                Bumblepig.ExplodeBullet();
                            }
                        }      

                    }
                    #endregion Ingame Controls
                    //end nontransition in-game controls
                }
            }//end of in-game controls

            //attempts to control sound parameters
            if (newState.IsKeyDown(Keys.NumPad1) && !oldState.IsKeyDown(Keys.NumPad1))
            {
                if (musicVolume > 0.1f)
                {
                    musicVolume -= 0.1f;
                }
                else musicVolume = 0.0f;
            }
            if (newState.IsKeyDown(Keys.NumPad2) && !oldState.IsKeyDown(Keys.NumPad2))
            {
                if (musicVolume < 0.9f)
                {
                    musicVolume += 0.1f;
                }
                else musicVolume = 1.0f;
            }
            if (newState.IsKeyDown(Keys.NumPad4) && !oldState.IsKeyDown(Keys.NumPad4))
            {
                if (sfxVolume > 0.1f)
                {
                    sfxVolume -= 0.1f;
                }
                else sfxVolume = 0.0f;
            }
            if (newState.IsKeyDown(Keys.NumPad5) && !oldState.IsKeyDown(Keys.NumPad5))
            {
                if (sfxVolume < 0.9f)
                {
                    sfxVolume += 0.1f;
                }
                else sfxVolume = 1.0f;
            }

            //main menu selection controls
            if (!stateTransition)
            {
                if (gameState == 0)
                {
                    if ((newState.IsKeyDown(Keys.Up) && !oldState.IsKeyDown(Keys.Up)) ||
                        (currentGamePadState.DPad.Up == ButtonState.Pressed && previousGamePadState.DPad.Up == ButtonState.Released) || (currentGamePadState.ThumbSticks.Left.Y > 0.1 && previousGamePadState.ThumbSticks.Left.Y < 0.1))
                    {   //move the highlighted button up one selection if up is pressed
                        menuSelection -= 1;
                        if (menuSelection < 0) //loop around to the bottom if up is pushed on the top option
                        {
                            menuSelection = maxMenuSelection;
                        }
                        sfxBank.PlayCue("ButtonSwitch");
                        //selectionChanged = true;
                    }

                    if ((newState.IsKeyDown(Keys.Down) && !oldState.IsKeyDown(Keys.Down)) ||
                        (currentGamePadState.DPad.Down == ButtonState.Pressed && previousGamePadState.DPad.Down == ButtonState.Released) || (currentGamePadState.ThumbSticks.Left.Y < -0.1 && previousGamePadState.ThumbSticks.Left.Y > -0.1))
                    {   //move it down one if down is pushed
                        menuSelection += 1;
                        if (menuSelection > maxMenuSelection)
                        {
                            menuSelection = 0;
                        }
                        sfxBank.PlayCue("ButtonSwitch");
                    }

                    if ((newState.IsKeyDown(Keys.Enter) && !oldState.IsKeyDown(Keys.Enter)) ||
                        (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A == ButtonState.Released))
                    {   //select an button by pressing enter
                        buttonSelected = true;
                        sfxBank.PlayCue("ButtonSelect");
                    }
                    //gamepad game exit needs some work, sends user from gameplay back to windows without hitting the menu first
                    if (newState.IsKeyDown(Keys.Escape) && oldState.IsKeyUp(Keys.Escape) ||
                        (currentGamePadState.Buttons.B == ButtonState.Pressed && previousGamePadState.Buttons.B == ButtonState.Released))
                    {
                        //this needs some work, user must press escape twice to exit the game from the main menu.  should only require one press
                        if (menuBack || MainMenu.onMainMenu)
                        { this.Exit(); }
                        menuBack = true;
                        //this.Exit();
                    }
                }

                //results screen controls
                if (gameState == 2 && resultsScreen.readyForButtons)
                {
                    if ((newState.IsKeyDown(Keys.Enter) && !oldState.IsKeyDown(Keys.Enter)) ||
                        (currentGamePadState.Buttons.A == ButtonState.Pressed && previousGamePadState.Buttons.A == ButtonState.Released))
                    {
                        flowerTotals.blue = 0;
                        flowerTotals.red = 0;
                        flowerTotals.yellow = 0;
                        flowerTotals.green = 0;
                        flowerTotals.orange = 0;
                        flowerTotals.purple = 0;
                        resultsScreen.visibleFlowers = 0;
                        if (resultsScreen.onNextButton)
                        {
                            ScreenFade.Begin(Screen.Game, FadeType.FadeOut, 0.5f, musicVolume);
                            gameLevel++;
                        }
                        else
                        {
                            ScreenFade.Begin(Screen.Menu, FadeType.FadeOut, 0.5f, musicVolume);
                        }
                        sfxBank.PlayCue("ButtonSelect");
                    }

                    if ((newState.IsKeyDown(Keys.Down) && !oldState.IsKeyDown(Keys.Down)) ||
                        (newState.IsKeyDown(Keys.Up) && !oldState.IsKeyDown(Keys.Up)) ||
                        (currentGamePadState.DPad.Down == ButtonState.Pressed && previousGamePadState.DPad.Down == ButtonState.Released) ||
                        (currentGamePadState.DPad.Up == ButtonState.Pressed && previousGamePadState.DPad.Up == ButtonState.Released) ||
                        (currentGamePadState.ThumbSticks.Left.Y < -0.1 && previousGamePadState.ThumbSticks.Left.Y > -0.1) ||
                        (currentGamePadState.ThumbSticks.Left.Y > 0.1 && previousGamePadState.ThumbSticks.Left.Y < 0.1))
                    {
                        if (resultsScreen.onNextButton) resultsScreen.onNextButton = false;
                        else resultsScreen.onNextButton = true;
                        sfxBank.PlayCue("ButtonSwitch");
                    }
                }



            }
            oldState = newState;
            previousGamePadState = currentGamePadState;

        }
        #endregion

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.Black);
            //float totalTime = totalTime + (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (!stateTransition)
            {
                if (gameState == 0)
                {
                    gSaturate = 255;
                    globalBatch.Begin();
                    MainMenu.DrawMenu(globalBatch, menuSelection);
                    //draw stats menu specific text here
                    if (MainMenu.onStatsMenu)
                    {
                        //starting y value for hi score column
                        //int y = 200;
                        //draws total money in the stats menu
                        //drawText(Color.Black, 300, 140, "$ " +Bumblepig.floatcoins.ToString());
                        globalBatch.DrawString(font, "$ " + Bumblepig.floatcoins.ToString(), new Vector2(300, 140), Color.Black);
                        ////draw all the hiscores in the hiscore list... right now, saving doesn't work, so this will crash
                        //foreach (int i in saveData.HiScore)
                        //{
                        //    drawText(Color.Black, 500, y, i.ToString());
                        //    y += 50;
                        //}
                    }
                    //globalBatch.Draw(menuTexture, menuRectangle, Color.White); 
                    globalBatch.End();
                }

                //draw level 1 if it is active
                if (gameState == 1)
                {
                    //int x = 0;
                    //BRENT EDIT - THIS ENTIRE SCOPE//
                    //Drawing the level regularly
                    graphics.GraphicsDevice.SetRenderTarget(0, renderTarget);
                    gSaturate = 255;
                    globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                  SpriteSortMode.Immediate,
                                  SaveStateMode.None);

                    TestLevel.Draw(globalBatch, Bumblepig);
                    globalBatch.End();

                    //grabbing the area rendered to
                    graphics.GraphicsDevice.SetRenderTarget(0, voidTarget);
                    texturedRenderedTo = renderTarget.GetTexture();
                    texturedRenderedToFirespray = texturedRenderedTo;
                    texturedRenderedToKidHand = texturedRenderedTo;
                    textureRenderedToFullScreen = texturedRenderedTo;

                    //cutting bumble pig out of all that
                    float fXincriment = 0.0f;
                    graphics.GraphicsDevice.SetRenderTarget(0, renderTarget2);
                    sprite.Begin(SpriteBlendMode.AlphaBlend,
                                  SpriteSortMode.Immediate,
                                  SaveStateMode.None);
                    foreach (Bug bug in TestLevel.BugList)
                    {
                        if (bug.isFireFly)
                        {
                            sprite.Draw(texturedRenderedTo, new Vector2(fXincriment, 0.0f), new Rectangle((int)bug.SpritePosition.X - 100, (int)bug.SpritePosition.Y - 100, 200, 200),
                                    new Color(255, 255, 255, 255), 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1);
                            fXincriment += 200;
                        }
                    }
                    sprite.End();

                    graphics.GraphicsDevice.SetRenderTarget(0, voidTarget);
                    texturedRenderedTo = renderTarget2.GetTexture();

                    graphics.GraphicsDevice.SetRenderTarget(0, renderTargetLighter);
                    sprite2.Begin(SpriteBlendMode.AlphaBlend,
                                  SpriteSortMode.Immediate,
                                  SaveStateMode.None);
                    if (TestLevel.endsequence && gameLevel == 2)
                    {
                        sprite2.Draw(texturedRenderedToKidHand, new Vector2(0.0f, 0.0f), new Rectangle((int)TestLevel.KidLighter.SpritePosition.X - 128, (int)TestLevel.KidLighter.SpritePosition.Y - 128, 256, 256),
                                new Color(255, 255, 255, 255), 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1);
                    }
                    sprite2.End();

                    graphics.GraphicsDevice.SetRenderTarget(0, voidTarget);
                    texturedRenderedToKidHand = renderTargetLighter.GetTexture();

                    graphics.GraphicsDevice.SetRenderTarget(0, renderTargetFirespray);
                    sprite3.Begin(SpriteBlendMode.AlphaBlend,
                                  SpriteSortMode.Immediate,
                                  SaveStateMode.None);
                    if (TestLevel.endsequence && gameLevel == 2)
                    {
                        sprite3.Draw(texturedRenderedToFirespray, new Vector2(0.0f, 0.0f), new Rectangle((int)TestLevel.FireCloud.SpritePosition.X - 128, (int)(TestLevel.FireCloud.SpritePosition.Y - (0.5 * TestLevel.FireCloud.recHeight)), 256, (int)TestLevel.FireCloud.recHeight),
                                new Color(255, 255, 255, 255), 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1);
                    }
                    sprite3.End();

                    graphics.GraphicsDevice.SetRenderTarget(0, voidTarget);
                    texturedRenderedToFirespray = renderTargetFirespray.GetTexture();

                    graphics.GraphicsDevice.SetRenderTarget(0, null);

                    //gameLevel = 2;
                    if (gameLevel == 0) //Draw with no desaturation when on the 1st level
                    {
                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        //TestLevel.Draw(globalBatch, Bumblepig);
                        globalBatch.Draw(textureRenderedToFullScreen, new Vector2(0.0f, 0.0f), Color.White);//new Color(10, 10, 10, (byte)gSaturate));
                        TestLevel.DrawHUD(globalBatch);
                        globalBatch.End();
                    }

                    if (gameLevel == 1) //Partially desaturate when on the 2nd level
                    {
                        gSaturate = gSaturateDusk; //this makes it somewhat desaturated
                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        lightEffect.Begin();
                        lightEffect.CurrentTechnique.Passes[0].Begin();
                        //TestLevel.Draw(globalBatch, Bumblepig);
                        globalBatch.Draw(textureRenderedToFullScreen, new Vector2(0.0f, 0.0f), new Color(0, 0, 0, (byte)gSaturate));
                        globalBatch.End();
                        lightEffect.CurrentTechnique.Passes[0].End();
                        lightEffect.End();

                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        //globalBatch.Draw(darkFilter, new Vector2(0.0f, 0.0f), new Color(105, 105, 255, 110));
                        globalBatch.Draw(orangeFilter, new Vector2(0.0f, 0.0f), Color.White);
                        globalBatch.End();

                        graphics.GraphicsDevice.Textures[1] = lightorb;
                        sprite2.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                        mask.Begin();
                        mask.CurrentTechnique.Passes[0].Begin();
                        int nXincriment = 0;
                        for (int n = 0; n < TestLevel.BugList.Count; n++)
                        {
                            if (TestLevel.BugList[n].isFireFly)
                            {
                                sprite2.Draw(texturedRenderedTo, new Vector2(TestLevel.BugList[n].SpritePosition.X - 100.0f, TestLevel.BugList[n].SpritePosition.Y - 100.0f), new Rectangle(nXincriment, 0, 200, 200),
                                        new Color(255, 255, 255, 255), 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1);
                                nXincriment += 200;
                            }
                        }
                        sprite2.End();
                        mask.CurrentTechnique.Passes[0].End();
                        mask.End();
                        gSaturate = 255;
                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        TestLevel.DrawHUD(globalBatch);
                        globalBatch.End();
                    }
                    
                    //graphics.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
                    if (gameLevel == 2) //Fully desaturate when on the boss level
                    {                        

                        //FINAL PASS - DRAWING THE LEVEL AS IT IS SEEN BY PLAYER//
                        gSaturate = gSaturateNight; //this makes it fully desaturated
                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        lightEffect.Begin();
                        lightEffect.CurrentTechnique.Passes[0].Begin();

                        //TestLevel.Draw(globalBatch, Bumblepig);
                        globalBatch.Draw(textureRenderedToFullScreen, new Vector2(0.0f, 0.0f), new Color(0, 0, 0, (byte)gSaturate));
                        globalBatch.End();
                        lightEffect.CurrentTechnique.Passes[0].End();
                        lightEffect.End();

                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        globalBatch.Draw(darkFilter, new Vector2(0.0f, 0.0f), new Color(105, 105, 255, 110));
                        globalBatch.End();

                        graphics.GraphicsDevice.Textures[1] = lightorb;
                        sprite2.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                        mask.Begin();
                        mask.CurrentTechnique.Passes[0].Begin();
                        int nXincriment = 0;
                        for (int n = 0; n < TestLevel.BugList.Count; n++)
                        {
                            if (TestLevel.BugList[n].isFireFly)
                            {
                                sprite2.Draw(texturedRenderedTo, new Vector2(TestLevel.BugList[n].SpritePosition.X - 100.0f, TestLevel.BugList[n].SpritePosition.Y - 100.0f), new Rectangle(nXincriment, 0, 200, 200),
                                        new Color(255, 255, 255, 255), 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1);
                                nXincriment += 200;
                            }
                        }
                        
                        sprite2.End();
                        mask.CurrentTechnique.Passes[0].End();
                        mask.End();

                        graphics.GraphicsDevice.Textures[1] = bossFiresprayLightorb;
                        sprite3.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                        mask.Begin();
                        mask.CurrentTechnique.Passes[0].Begin();

                        if (TestLevel.endsequence && TestLevel.isFireSpraying)
                        {
                            sprite3.Draw(texturedRenderedToFirespray, new Vector2(TestLevel.FireCloud.SpritePosition.X - 128.0f, (TestLevel.FireCloud.SpritePosition.Y - 256)), new Rectangle(0, (512 - (int)TestLevel.FireCloud.recHeight), 256, (int)TestLevel.FireCloud.recHeight),
                                    new Color(255, 255, 255, 255), 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1);
                        }

                        sprite3.End();
                        mask.CurrentTechnique.Passes[0].End();
                        mask.End();

                        graphics.GraphicsDevice.Textures[1] = bossLighterLightorb;
                        sprite4.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                        mask.Begin();
                        mask.CurrentTechnique.Passes[0].Begin();

                        if (TestLevel.endsequence)
                        {
                            sprite4.Draw(texturedRenderedToKidHand, new Vector2(TestLevel.KidLighter.SpritePosition.X - 128.0f, TestLevel.KidLighter.SpritePosition.Y - 128.0f), new Rectangle(0, 0, 256, 256),
                                            new Color(255, 255, 255, 255), 0, new Vector2(0, 0), 1.0f, SpriteEffects.None, 1);
                        }

                        sprite4.End();
                        mask.CurrentTechnique.Passes[0].End();
                        mask.End();

                        gSaturate = 255;
                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        TestLevel.DrawHUD(globalBatch);
                        globalBatch.End();
                        
                    }
                }

                //draws score and life if game isn't on the menu screen.  Needs to be on top so it should be drawn last.
                if (gameState == 1)
                {
                    globalBatch.Begin();
                    //drawText(Color.Black, 400, 700, "Score: " + Bumblepig.score.ToString());
                    //drawText(Color.Black, 300, 700, "Life: " + Bumblepig.life.ToString());
                    //drawText(Color.Black, 550, 700, "Confidence Meter: " + TestLevel.fConfidenceMeter.ToString());
                    //drawText(Color.Black, 200, 730, "$ " + Bumblepig.floatcoins.ToString());
                    globalBatch.DrawString(font, "$ " + Bumblepig.floatcoins.ToString(), new Vector2(200, 730), Color.Black);
                    //drawText(Color.Black, 650, 700, "Streak: " + TestLevel.nStreakCount.ToString());

                    //draw 'pause' on screen if the game is paused
                    if (gameState > 0 && paused)
                    {
                        //drawText(Color.Black, 512, 384, "PAUSED");
                        globalBatch.DrawString(font, "PAUSED", new Vector2(512, 384), Color.Black);
                    }
                    globalBatch.End();
                }

                
                //draw results screen
                if (gameState == 2)
                {
                    // keep the same saturation level for results screen as for the level
                    if (gameLevel == 0)
                    {
                        gSaturate = 255;
                        globalBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
                        resultsScreen.DrawGarden(globalBatch);
                        globalBatch.End();
                    }
                    if (gameLevel == 1)
                    {
                        SpriteBatch sprite2 = new SpriteBatch(graphics.GraphicsDevice);

                        gSaturate = gSaturateDusk; //this makes it somewhat desaturated
                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        lightEffect.Begin();
                        lightEffect.CurrentTechnique.Passes[0].Begin();
                        resultsScreen.DrawGarden(globalBatch);
                        globalBatch.End();
                        lightEffect.CurrentTechnique.Passes[0].End();
                        lightEffect.End();

                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        //globalBatch.Draw(darkFilter, new Vector2(0.0f, 0.0f), new Color(105, 105, 255, 110));
                        globalBatch.Draw(orangeFilter, new Vector2(0.0f, 0.0f), Color.White);
                        globalBatch.End();

                        graphics.GraphicsDevice.Textures[1] = lightorb;
                        sprite2.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                        mask.Begin();
                        mask.CurrentTechnique.Passes[0].Begin();
                        sprite2.Draw(texturedRenderedTo, new Vector2(Bumblepig.spritePosition.X - 100.0f, Bumblepig.spritePosition.Y - 100.0f), new Color(255, 255, 255, 255));
                        sprite2.End();
                        mask.CurrentTechnique.Passes[0].End();
                        mask.End();
                    }
                    if (gameLevel == 2)
                    {
                        SpriteBatch sprite2 = new SpriteBatch(graphics.GraphicsDevice);

                        //FINAL PASS - DRAWING THE LEVEL AS IT IS SEEN BY PLAYER//
                        gSaturate = gSaturateNight; //this makes it fully desaturated
                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        lightEffect.Begin();
                        lightEffect.CurrentTechnique.Passes[0].Begin();
                        resultsScreen.DrawGarden(globalBatch);
                        globalBatch.End();
                        lightEffect.CurrentTechnique.Passes[0].End();
                        lightEffect.End();

                        globalBatch.Begin(SpriteBlendMode.AlphaBlend,
                                      SpriteSortMode.Immediate,
                                      SaveStateMode.None);
                        globalBatch.Draw(darkFilter, new Vector2(0.0f, 0.0f), new Color(105, 105, 255, 110));
                        globalBatch.End();

                        graphics.GraphicsDevice.Textures[1] = lightorb;
                        sprite2.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                        mask.Begin();
                        mask.CurrentTechnique.Passes[0].Begin();
                        sprite2.Draw(texturedRenderedTo, new Vector2(Bumblepig.spritePosition.X - 100.0f, Bumblepig.spritePosition.Y - 100.0f), new Color(255, 255, 255, 255));
                        sprite2.End();
                        mask.CurrentTechnique.Passes[0].End();
                        mask.End();
                    }                     
                    globalBatch.Begin();
                    //Change the saturation back so the score display isn't affected and illegible
                    gSaturate = 255;
                    resultsScreen.DrawScore(globalBatch);
                    if (resultsScreen.ScoreBoard.SpriteScale == 1.0f)
                    {
                        //drawText(Color.Black, 376, 306, resultsScreen.red.ToString());
                        globalBatch.DrawString(font, resultsScreen.red.ToString(), new Vector2(376, 306), Color.Black);
                        //drawText(Color.Black, 542, 306, resultsScreen.blue.ToString());
                        globalBatch.DrawString(font, resultsScreen.blue.ToString(), new Vector2(542, 306), Color.Black);
                        //drawText(Color.Black, 706, 306, resultsScreen.yellow.ToString());
                        globalBatch.DrawString(font, resultsScreen.yellow.ToString(), new Vector2(706, 306), Color.Black);
                        //drawText(Color.Black, 376, 396, resultsScreen.purple.ToString());
                        globalBatch.DrawString(font, resultsScreen.purple.ToString(), new Vector2(376, 396), Color.Black);
                        //drawText(Color.Black, 542, 396, resultsScreen.green.ToString());
                        globalBatch.DrawString(font, resultsScreen.green.ToString(), new Vector2(542, 396), Color.Black);
                        //drawText(Color.Black, 706, 396, resultsScreen.orange.ToString());
                        globalBatch.DrawString(font, resultsScreen.orange.ToString(), new Vector2(706, 396), Color.Black);
                        //drawText(Color.Black, 376, 456, Bumblepig.score.ToString());
                        globalBatch.DrawString(font, Bumblepig.score.ToString(), new Vector2(376, 456), Color.Black);
                        //drawText(Color.Black, 376, 486, "$" +Bumblepig.floatcoins.ToString());
                        globalBatch.DrawString(font, "$" + Bumblepig.floatcoins.ToString(), new Vector2(376, 486), Color.Black);
                    }
                    globalBatch.End();
                }

                ScreenFade.Draw();

            }
            

            
            base.Draw(gameTime);
        }

        

        //public void drawText(Color aColor, int x, int y, string str)
        //{
        //    m_fontTimes.KernEnable = true;
        //    int nWidth = m_fontTimes.MeasureString(str);
        //    m_fontTimes.DrawString(x, y, aColor, str, nWidth);
        //}

        //Save and load data struct and functions below
        public struct SaveGameData
        {
            public int coins;
            public int life;
            public List<int> HiScore;
        }

        //public void Save(SaveGameData gameData, int score)
        //{
        //    //manipulate hi score list here, try to sort them and limit to 5 eventually
        //    gameData.HiScore.Add(score);
        //    gameData.HiScore.Sort();
        //    gameData.HiScore.Reverse();

        //    //int currentListIndex = 0;
        //    for (int i = gameData.HiScore.Count; i > 0; i-- )
        //    {
        //        if (i > 5)
        //        {
        //            gameData.HiScore.RemoveAt(i - 1);
        //        }
        //        //currentListIndex++;
        //    }
        //    //This no longer works in XNA 2.0
        //    //StorageDevice testDevice = StorageDevice.ShowStorageDeviceGuide();

        //    //open container
        //    StorageContainer container = testDevice.OpenContainer("TestStorage");

        //    //get full path name
        //    string filename = Path.Combine(container.Path, "savegame.xml");

        //    // Open the file, creating it if necessary
        //    FileStream stream = File.Open(filename, FileMode.Create);

        //    // Convert the object to XML data and put it in the stream
        //    XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
        //    serializer.Serialize(stream, gameData);

        //    // Close the file
        //    stream.Close();

        //    // Dispose the container, to commit changes
        //    container.Dispose();        
        //}

        //public SaveGameData Load()
        //{
        //    SaveGameData ret = new SaveGameData();
        //    ret.HiScore = new List<int>();
        //    //this no longer works in XNA 2.0
        //    //StorageDevice testDevice = StorageDevice.ShowStorageDeviceGuide();

        //    // Open a storage container
        //    StorageContainer container = testDevice.OpenContainer("TestStorage");

        //    // Get the path of the save game
        //    string filename = Path.Combine(container.Path, "savegame.xml");

        //    // Check to see if the save exists
        //    if (!File.Exists(filename))
        //    {    // Notify the user there is no save  
        //        for (int i = 0; i < 5; i++)
        //        {
        //            ret.HiScore.Add(0);
        //        }
        //        return ret;
        //    }

        //    // Open the file
        //    FileStream stream = File.Open(filename, FileMode.OpenOrCreate,
        //        FileAccess.Read);

        //    // Read the data from the file
        //    XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
        //    ret = (SaveGameData)serializer.Deserialize(stream);

        //    // Close the file
        //    stream.Close();

        //    // Dispose the container
        //    container.Dispose();

        //    return ret;
        //}

        //function to read in the level layout from a text file
        public List<List<char>> levelLayout(string path)
        { 
            List<char> columns = new List<char>();
            List<List<char>> rows = new List<List<char>>();
            StreamReader sr = new StreamReader(path);
            string str = sr.ReadToEnd();
            sr.Close();
            char[] chars = str.ToCharArray();
            foreach (char ch in chars)
            {
                if (ch != ' ' && ch != '\r' && ch != '\n' && ch != '\t') //ignore spacing characters
                {
                    if (ch != '.') //use periods to index rows of the map
                    {
                        columns.Add(ch);
                    }
                    else
                    {
                        rows.Add(columns);
                        columns = new List<char>();
                    }
                }
            }            
            return rows;
        }
    }
    
    public struct FlowerTotals
    {
        public int yellow, red, blue, orange, purple, green;        
    }
    public struct LevelTextures //use to indicate level specific texture and animation details
    {
        public string blueFlowerMale, blueFlowerFemale, redFlowerMale, redFlowerFemale, yellowFlowerMale, yellowFlowerFemale,
                      BGElement0, BGElement1, BGElement2, BGElement3, BGElement4, BGElement5, BGElement6, BGElement7,
                      BGElement8, BGElement9;
        //mess of ints and bools for the background elements animation info...bleck...
        public int BGElement0framesWide, BGElement0framesTall, BGElement0lastFrame, BGElement0framesPerSec, BGElement1framesWide, BGElement1framesTall,
                   BGElement1lastFrame, BGElement1framesPerSec, BGElement2framesWide, BGElement2framesTall, BGElement2lastFrame, BGElement2framesPerSec, BGElement3framesWide,
                   BGElement3framesTall, BGElement3lastFrame, BGElement3framesPerSec, BGElement4framesWide, BGElement4framesTall, BGElement4lastFrame, BGElement4framesPerSec,
                   BGElement5framesWide, BGElement5framesTall, BGElement5lastFrame, BGElement5framesPerSec, BGElement6framesWide, BGElement6framesTall,
                   BGElement6lastFrame, BGElement6framesPerSec, BGElement7framesWide, BGElement7framesTall, BGElement7lastFrame, BGElement7framesPerSec, BGElement8framesWide,
                   BGElement8framesTall, BGElement8lastFrame, BGElement8framesPerSec, BGElement9framesWide, BGElement9framesTall, BGElement9lastFrame, BGElement9framesPerSec;
        public bool BGElement0sym, BGElement1sym, BGElement2sym, BGElement3sym, BGElement4sym, BGElement5sym, BGElement6sym,
                    BGElement7sym, BGElement8sym, BGElement9sym;
    }


    //the shit out of this
    public enum FadeType { FadeIn, FadeOut };
    public enum Screen { Menu, Game, Results };
    public struct tagScreenFade
    {
        public bool Fade;
        public FadeType Type;
        public Screen ScreenSelect;
        public float Time;
        public float OldTime;
        float Alpha;
        float AlphaIncrement;
        float fMusicVolume;
        float fMusicVolumeIncrement;
        Texture2D blackFade;
        SpriteBatch fadeBatch;
        Rectangle posRect;

        public void Initialize(ContentManager content, GraphicsDevice gDevice)
        {
            Fade = false;
            Time = 0.0f;
            OldTime = 0.0f;
            blackFade = content.Load<Texture2D>("Images/blackfade");
            fadeBatch = new SpriteBatch(gDevice);
            posRect = new Rectangle(0, 0, blackFade.Width, blackFade.Height);
        }

        public void Begin(Screen newScreenSelect, FadeType newType, float newTime, float musicVolume)
        {
            ScreenSelect = newScreenSelect;
            Fade = true;
            Type = newType;
            Time = newTime;
            OldTime = newTime;
            AlphaIncrement = 255.0f / Time;
            fMusicVolume = musicVolume;
            fMusicVolumeIncrement = musicVolume / Time;
            if (Type == FadeType.FadeOut)
            {
                Alpha = 0.0f;
            }
            else
            {
                Alpha = 255.0f;
            }
        }

        public void Update(float elapsed, ref int gameState, ref bool stateTransition, ref ContentManager level1Content, ref float musicVolume)
        {
            if (Time > 0.0f)
            {
                Time -= elapsed;
                if (Type == FadeType.FadeOut)
                {
                    Alpha += (AlphaIncrement * elapsed);
                    if (Alpha > 255.0f)
                        Alpha = 255.0f;
                    //fade music out along with screen
                    musicVolume -= (fMusicVolumeIncrement * elapsed);
                    if (musicVolume < 0.0f) musicVolume = 0.0f; //cap the low end at zero
                }
                else
                {
                    Alpha -= (AlphaIncrement * elapsed);
                    if (Alpha < 0.0f)
                        Alpha = 0.0f;
                    //fade music in along with screen
                    musicVolume += (fMusicVolumeIncrement * elapsed);
                    if (musicVolume > fMusicVolume) musicVolume = fMusicVolume; //cap the high end at the volume it was set to before the fading
                }
            }
            else
            {
                Time = 0.0f;
                Fade = false;

                if (gameState != (int)ScreenSelect)
                {
                    this.Begin(ScreenSelect, FadeType.FadeIn, OldTime, fMusicVolume);
                    gameState = (int)ScreenSelect;
                    stateTransition = true;
                    level1Content.Unload();                    
                }
            }
        }

        public void Draw()
        {
            fadeBatch.Begin();
            fadeBatch.Draw(blackFade, posRect, new Color(255,255,255,(byte)Alpha)); 
            fadeBatch.End();
        }
    }

    


}



